import {Component, OnInit} from "@angular/core";
import {JSSBDownloadService} from "./jssb.download.service";
import {JSSBDownloadFile} from "../model/JSSBDownloadFile";


@Component({
    selector: 'my-rightContent',
    templateUrl:"./jssb.download.component.html",
    styleUrls:["./jssb.download.component.css"],
    providers:[
        JSSBDownloadService
    ]
})
export class JSSBDownLoadComponent implements OnInit{

    _datasource:JSSBDownloadFile[]=new Array();
    _message:string="";

    set DataSource(value:Array<JSSBDownloadFile>){
        if(value==null) this._datasource=new Array();
        this._datasource=value.map(value=>{
           let newValue=new JSSBDownloadFile();
           newValue.CopyFrom(value);
           return newValue;
        });

    }

    set Message(value:string){

        let m=(value||"").trim();
        if(m==""){
            this._message=m;
        }
        else {
            this._message=value;
        }
    }

    get Message():string{
        return this._message;
    }


    ngOnInit(): void {
        this.donwloadService.getFilesList()
            .then(response=>{
                if(response.result){
                    this.DataSource=response.data;
                }
            })
            .catch(ex=>this.SetMessage(ex));

    }

    constructor(private donwloadService:JSSBDownloadService){}

    downloadFile(id:number) {
        return this.donwloadService.getFile(id);
    }


    SetMessage(ex:any,message:string=null):void{
        if(ex) this._message=ex.message;
        if(message) this._message=(this._message||"")+message;
    }



}
